home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 103 / CD-ROM 103.iso / edu / martianwin / src / game.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-08-11  |  5.4 KB  |  256 lines

  1. #include"fx.c"
  2. #include"gameover.c"
  3.  
  4. int play_loop(int type);
  5. void shuffle()
  6. {
  7.     srand(time(NULL));
  8.     int done;
  9.     int x,y, n;
  10.     for(y=0; y<chips_y; y++)
  11.     for(x=0; x<chips_x; x++)
  12.     {
  13.         mchip[x][y].type=0;
  14.         mchip[x][y].side=0; // ponemos todas la chips boca abajo
  15.         mchip[x][y].state=0; // el estado de todas las chips es "sin descubrir"
  16.     }
  17.  
  18.     int loop=0;
  19.     while(loop<2)
  20.     {
  21.     for(n=1; n<=(chips/2);n++)
  22.     {
  23.         done=0;
  24.         while(done==0)
  25.         {
  26.         x=rand()%chips_x;
  27.         y=rand()%chips_y;
  28.         if(mchip[x][y].type==0)
  29.         {
  30.             mchip[x][y].type=n;
  31.             done=1;
  32.         }
  33.         }
  34.     }
  35.     loop++;
  36.     }
  37.     turn=0;
  38. }
  39.  
  40. void set_screen()
  41. {
  42.     SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format,0,0,0));
  43. }
  44.  
  45. void print_panel()
  46. {
  47.     int minutes=int(gametime/60);
  48.     int seconds=int(gametime-minutes*60);
  49.     if(seconds<10)
  50.     T_Print(scorefont, 10, floor+32, "TIME: %d:0%d", minutes, seconds);
  51.     else
  52.     T_Print(scorefont, 10, floor+32, "TIME: %d:%d", minutes, seconds);
  53.     T_Print(scorefont, 300, floor+32, "SCORE: %d", score);
  54. }
  55.  
  56. void display_combo()
  57. {
  58.     showtime=0;
  59.     char * bonus;
  60.     int offset=0;
  61.     if(combo==2)   bonus="  NICE   ";
  62.     if(combo==3) { bonus="  GREAT  "; offset=-(bonusfont->w/91)/2;}
  63.     if(combo==4) { bonus="EXCELLENT"; offset=-(bonusfont->w/91)/2;}
  64.     if(combo>=5)   bonus="TERRIFIC ";
  65.     Mix_PlayChannel(4,combo_sound,0);
  66.     SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format,255,255,255));
  67.     SDL_Flip(screen);
  68.     SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format,0,0,0));
  69.     #define combo_x 208
  70.     #define combo_y 200
  71.     #define combo_w 320
  72.     #define combo_h 80
  73.     init_combo_fx(combo_x, combo_y, combo_w, combo_h);
  74.     while(showtime<3)
  75.     {
  76.         showtime+=dt;
  77.     SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format,0,0,0));
  78.         play_loop(2); // el 2 indica que no chequee eventos, no actualice la pantalla y no incremente el tiempo
  79.     combo_fx();
  80.     T_Print(bonusfont, combo_x+offset,combo_y, "%s", bonus);
  81.     T_Print(bonusfont, combo_x,combo_y+40,"%dX Combo", combo);
  82.     SDL_Flip(screen);
  83.     }
  84.     SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format,0,0,0));
  85.     flush_events();
  86. }
  87.  
  88. void print_screen(SDL_Surface *surface)
  89. {
  90.     print_panel();
  91.     int x, y;
  92.     SDL_Rect src, dst;
  93.     src.x=0; src.w=chip_w; src.h=chip_h;
  94.     for(y=0; y<chips_y; y++)
  95.     for(x=0; x<chips_x; x++)
  96.     {
  97.         dst.x=chips_start_x+(x*(chip_w+chips_spacing_x));
  98.         dst.y=chips_start_y+(y*(chip_h+chips_spacing_y));
  99.  
  100.         if(mchip[x][y].state==1) mchip[x][y].side=1;
  101.         if(mchip[x][y].side==0)
  102.         src.y=0;
  103.         else
  104.         src.y=mchip[x][y].type*chip_h;
  105.         SDL_BlitSurface(Schips, &src, surface, &dst);
  106.     }
  107. }
  108.  
  109. void anim()
  110. {
  111.     draw_stars();
  112.     draw_bars();
  113. }
  114.  
  115. int chk_complete()
  116. {
  117.     int complete=1;
  118.     int x,y;
  119.     for(y=0; y<chips_y; y++)
  120.     for(x=0; x<chips_x; x++)
  121.         if(mchip[x][y].state==0)
  122.         complete=0;
  123.     return(complete);
  124. }
  125.  
  126. void chk_mouse()
  127. {
  128.     int x, y; //posicion del mouse
  129.     int xx, yy;
  130.     SDL_GetMouseState(&x, &y);
  131.     x-=chips_start_x; y-=chips_start_y;
  132.     for(yy=0; yy<chips_y; yy++)
  133.     for(xx=0; xx<chips_x; xx++)
  134.     {
  135.         if(x>=(xx*(chip_w+chips_spacing_x)) && x<=(xx*(chip_w+chips_spacing_x))+chip_w)
  136.         {
  137.         if(y>=(yy*(chip_h+chips_spacing_y)) && y<=(yy*(chip_h+chips_spacing_y))+chip_h)
  138.         {
  139.             if(mchip[xx][yy].side==0)
  140.             {
  141.             mchip[xx][yy].side=1;
  142.             Mix_PlayChannel(1,touch,0);
  143.             switch(turn)
  144.             {
  145.                 case 0:
  146.                 match_x=xx;
  147.                 match_y=yy;
  148.                 turn=1;
  149.                 break;
  150.                 case 1: // si es la segunda fase del turno nos fijamos si la chip recogida concuerda con la anterior
  151.                 mchip[xx][yy].side=1;
  152.                 if(mchip[xx][yy].type==mchip[match_x][match_y].type)
  153.                 {
  154. // --- MATCH
  155.                     mchip[xx][yy].state=1;
  156.                     mchip[match_x][match_y].state=1;
  157.                     score+=10;
  158.                     Mix_PlayChannel(2,match,0);
  159.                     turn=0;
  160.                     score+=combo*30;
  161.                     combo++;
  162.                     if(combo>1) display_combo();
  163.                 }
  164.                 else
  165.                 {
  166. // --- NO MATCH
  167.                     Mix_PlayChannel(2,nomatch,0);
  168.                     combo=0;
  169.                     turn=2;
  170.                 }
  171.                 break;
  172.             }
  173.             }
  174.         }
  175.         }
  176.         if(turn==2)
  177.         {
  178.         showtime=0;
  179.         while(showtime<1)
  180.         {
  181.             showtime+=dt;
  182.             play_loop(1); // el 1 indica que no chequee eventos
  183.             SDL_Flip(screen);
  184.         }
  185.         flush_events();
  186.         turn=0;
  187.         mchip[xx][yy].side=0;
  188.         mchip[match_x][match_y].side=0;
  189.         }
  190.     }
  191. }
  192.  
  193. int game_events(void)
  194. {
  195.     int action;
  196.     action=key_events();
  197.     return(action);
  198. }
  199.  
  200. int play_loop(int type)
  201. {
  202.     int action;
  203.     int wingame=0;
  204.     while(action!=Kexit && wingame==0 && action!=Kescape)
  205.     {
  206.     timing();
  207.     if(type!=2) gametime+=dt;
  208.     if(type==0) action=game_events();
  209.     play_music();
  210.         anim();
  211.         print_screen(screen);
  212.     if(type==0) SDL_Flip(screen);
  213.     SDL_Delay(5);
  214.     wingame=chk_complete(); // nos fijamos si se completo el puzzle
  215.     if(type!=0) break;
  216.     }
  217.     return(action);
  218. }
  219.  
  220. void init_fx()
  221. {
  222.     init_stars();
  223.     init_bars();
  224. }
  225.  
  226. int play()
  227. {
  228.     int action;
  229.     srand(time(NULL));
  230.     init_game();
  231.     flush_events();
  232.     shuffle();
  233.     init_fx();
  234.  
  235.     set_screen();
  236.     action=play_loop(0); // el 0 es la forma habitual de llamar a esta rutina, si fuera 1 no chequearie eventos
  237.     if(action!=Kexit) game_over();
  238.     close_game();
  239.     return(action);
  240. }
  241.  
  242. void screen_mode()
  243. {
  244.     if(fullscreen)
  245.     {
  246.         screen = SDL_SetVideoMode(screen_w,screen_h,screen_bpp,0);
  247.         fullscreen = 0;
  248.     }
  249.     else
  250.     {
  251.         screen = SDL_SetVideoMode(screen_w,screen_h,screen_bpp,SDL_FULLSCREEN);
  252.         fullscreen = 1;
  253.     }
  254.     SDL_BlitSurface(menuback, NULL, screen, NULL);
  255. }
  256.